﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace proLearningEnglish
{
    public partial class frmNewWordSense : Form
    {
        //private DataManager objDataManager = null;
        private TreeNode nodeSenseSelected = null;

        public frmNewWordSense()
        {
            InitializeComponent();       
        }
        
        private void frmNewWordSense_Load(object sender, EventArgs e)
        {
            cmbWordType.Items.AddRange(WordSense.WORDTYPELIST);
            cmbWordType.SelectedIndex = 0;          
        }

        private ComboTreeBox loadSense(String type)
        {
            ComboTreeBox combo = new ComboTreeBox();
            combo.Nodes.Clear();
            ComboTreeNode rootNode = new ComboTreeNode("Chọn nghĩa");
            List<WordSense> lst = frmMain.objDataManager.getIsARootWordSenseNodes(type);
            for (int i = 0; i < lst.Count; i++)
            {
                addNode(rootNode, lst[i]);
            }
            combo.Nodes.Add(rootNode);
            combo.SelectedNode = rootNode;
            combo.ExpandAll();
            return combo;
        }

        private void addNode(ComboTreeNode node, WordSense objWordSense)
        {
            ComboTreeNode nodeTmp = node.Nodes.Add(objWordSense.getID(), objWordSense.getLabel());
            nodeTmp.Tag = objWordSense;
            List<WordSense> lst = frmMain.objDataManager.getIsAChildWordSenseNodes(objWordSense);
            for (int i = 0; i < lst.Count; i++)
            {
                addNode(nodeTmp, lst[i]);
            }
        }

        private void btnAddSense_Click(object sender, EventArgs e)
        {
            if (txtWordSense.Text.Trim() == "")
            {
                MessageBox.Show("Nghĩa không thể rỗng!");
                return;
            }
            TreeNode nodeType = null;
            String wordType = WordSense.WORDTYPELIST[cmbWordType.SelectedIndex];
            if (treWordSenseDetail.Nodes.Find(wordType, false).Length == 0)
            {
                treWordSenseDetail.Nodes.Add(wordType, wordType);
            }
            nodeType = treWordSenseDetail.Nodes.Find(wordType, false)[0];
            TreeNode nodeSense = nodeType.Nodes.Add(txtWordSense.Text.Trim(), txtWordSense.Text.Trim());
            nodeSense.ForeColor = Color.Red;
            nodeSenseSelected = nodeSense;
            treWordSenseDetail.ExpandAll();
        }

        private void btnAddIsA_Click(object sender, EventArgs e)
        {
            TreeNode node = nodeSenseSelected.Nodes.Add("isa", "Is a: ");
            DropDownTreeNode nodeIsA = new DropDownTreeNode();
            nodeIsA.ControlUsedInNode = typeof(ComboTreeBox);
            nodeIsA.ForeColor = Color.Blue;
            nodeIsA.ComboTreeBox = this.loadSense(nodeSenseSelected.Parent.Text.Trim());
            node.Nodes.Add(nodeIsA);
            nodeSenseSelected.ExpandAll();
            btnAddIsA.Enabled = false;
            btnDeleteIsA.Enabled = true;
        }

        private void treWordSenseDetail_AfterSelect(object sender, TreeViewEventArgs e)
        {
            findSenseSelected(e.Node);

            btnAddIsA.Enabled = nodeSenseSelected.Nodes.ContainsKey("isa") ? false : true;
            btnDeleteIsA.Enabled = !btnAddIsA.Enabled;

            btnDeleteSyn.Enabled = nodeSenseSelected.Nodes.ContainsKey("synonym") ? true : false;
            btnDeleteAnto.Enabled = nodeSenseSelected.Nodes.ContainsKey("antonym") ? true : false;
            btnDeletePicture.Enabled = nodeSenseSelected.Nodes.ContainsKey("picture") ? true : false;
        }

        private void findSenseSelected(TreeNode node)
        {
            if (WordSense.WORDTYPELIST.Contains<String>(node.Text))
                return;
            if (WordSense.WORDTYPELIST.Contains<String>(node.Parent.Text))
                nodeSenseSelected = node;
            else findSenseSelected(node.Parent);
        }

        private void btnAddSyn_Click(object sender, EventArgs e)
        {
            TreeNode nodeSyn = 
                nodeSenseSelected.Nodes.ContainsKey("synonym") ? 
                nodeSenseSelected.Nodes.Find("synonym", false)[0] : 
                nodeSenseSelected.Nodes.Add("synonym", "Synonym: ");                    
            DropDownTreeNode combo = new DropDownTreeNode();
            combo.ControlUsedInNode = typeof(ComboTreeBox);
            combo.ForeColor = Color.Blue;
            combo.ComboTreeBox = loadSense(nodeSenseSelected.Parent.Text.Trim());
            nodeSyn.Nodes.Add(combo);
            treWordSenseDetail.ExpandAll();

            btnDeleteSyn.Enabled = true;
        }

        private void btnAddAnto_Click(object sender, EventArgs e)
        {
            TreeNode nodeSyn = 
                nodeSenseSelected.Nodes.ContainsKey("antonym") ? 
                nodeSenseSelected.Nodes.Find("antonym", false)[0] : 
                nodeSenseSelected.Nodes.Add("antonym", "Antonym: ");  
            DropDownTreeNode combo = new DropDownTreeNode();
            combo.ControlUsedInNode = typeof(ComboTreeBox);
            combo.ForeColor = Color.Blue;
            combo.ComboTreeBox = loadSense(nodeSenseSelected.Parent.Text.Trim());
            nodeSyn.Nodes.Add(combo);
            treWordSenseDetail.ExpandAll();

            btnDeleteAnto.Enabled = true;
        }

        private void btnDeleteIsA_Click(object sender, EventArgs e)
        {
            treWordSenseDetail.Nodes.Remove(nodeSenseSelected.Nodes.Find("isa", false)[0]);
            btnAddIsA.Enabled = true;
            btnDeleteIsA.Enabled = false;
        }

        private void btnDeleteSyn_Click(object sender, EventArgs e)
        {
            
        }

        private void btnDeleteSense_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("Bạn thực sự muốn xóa nghĩa được chọn?", "Xác nhận", MessageBoxButtons.YesNo);
            if (r == DialogResult.Yes)
                treWordSenseDetail.Nodes.Remove(nodeSenseSelected);
        }

        private void btnAddPicture_Click(object sender, EventArgs e)
        {

            TreeNode nodePic = nodeSenseSelected.Nodes.Add("picture", "Picture");
            DropDownTreeNode node = new DropDownTreeNode();
            node.ControlUsedInNode = typeof(OpenFileDialog);
            nodePic.Nodes.Add(node);            
            node.ForeColor = Color.Blue;
            treWordSenseDetail.ExpandAll();

            this.btnDeletePicture.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveWordSenses();
        }

        private void btnDeletePicture_Click(object sender, EventArgs e)
        {
            string key = "picture";
            if (nodeSenseSelected.Nodes.ContainsKey(key))
            {
                TreeNode node = nodeSenseSelected.Nodes.Find(key, false)[0];
                node.Remove();
            }
        }

        private void SaveWordSenses()
        {
            foreach (TreeNode nodeType in treWordSenseDetail.Nodes)
            {
                int type = frmMain.objDataManager.getWordSenseTypeFromString(nodeType.Text);
                String pic = "";
                String label = "";
                foreach (TreeNode nodeSense in nodeType.Nodes)
                {
                    label = nodeSense.Text;
                    if(nodeSense.Nodes.ContainsKey("picture"))
                    {
                        DropDownTreeNode n = (DropDownTreeNode)nodeSense.Nodes.Find("picture", false)[0].Nodes[0];
                        String pathPic = n.Tag.ToString();
                        pic = n.Text;
                    }
                    WordSense ws = new WordSense(type, label, pic);
                    frmMain.objDataManager.addWordSense(ws);
                    //get relation
                    if (nodeSense.Nodes.ContainsKey("isa"))
                    {
                        DropDownTreeNode n = (DropDownTreeNode)nodeSense.Nodes.Find("isa", false)[0].Nodes[0];
                        String senseIsA = n.Text;
                        WordSense s = new WordSense(type, senseIsA, "");
                        frmMain.objDataManager.addRelation(ws, frmMain.objDataManager.IsA, s);
                    }
                    //get synonym
                    if (nodeSense.Nodes.ContainsKey("synonym"))
                    {
                        TreeNode n = nodeSense.Nodes.Find("synonym", false)[0];
                        foreach (DropDownTreeNode syn in n.Nodes)
                        {
                            String senseSyn = syn.Text;
                            WordSense s = new WordSense(type, senseSyn, "");
                            frmMain.objDataManager.addRelation(ws, frmMain.objDataManager.SynonymWith, s);
                        }
                    }
                    //get antonym
                    if (nodeSense.Nodes.ContainsKey("antonym"))
                    {
                        TreeNode n = nodeSense.Nodes.Find("antonym", false)[0];
                        foreach (DropDownTreeNode anto in n.Nodes)
                        {
                            String senseAnto = anto.Text;
                            WordSense s = new WordSense(type, senseAnto, "");
                            frmMain.objDataManager.addRelation(ws, frmMain.objDataManager.AntonymWith, s);
                        }
                    }
                }
            }

            frmMain.objDataManager.save();
        }
    }
}
